package com.zuorenke.vertx.handler;

import com.zuorenke.help.ResponseUtils;
import com.zuorenke.vertx.VertxUtil;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author Z.R.K
 * @description
 * @create 2019-05-21 13:51:23
 **/
@Component
@Slf4j
public class RequestBusinessAsyncHandler implements Handler<RoutingContext> {
	
	@Resource
	private RequestBusinessHandler requestBusinessHandler;
	
	@Override
	public void handle(RoutingContext event) {
		VertxUtil.getVertxInstance().executeBlocking(
			future -> requestBusinessHandler.handle(event),
			future -> {
				if(future.failed()){
					log.error("invoke method fail {}", future.cause().getMessage());
					ResponseUtils.responseErrorMsg(event.response()
							, HttpResponseStatus.INTERNAL_SERVER_ERROR);
					future.cause().printStackTrace();
				}
			});
	}
}